
package cribbage.client;

import com.samskivert.swing.Controller;
import com.samskivert.swing.HGroupLayout;
import com.samskivert.swing.MultiLineLabel;
import com.samskivert.swing.VGroupLayout;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.crowd.client.PlacePanel;
import com.threerings.parlor.turn.client.TurnDisplay;
import com.threerings.toybox.client.ChatPanel;
import com.threerings.toybox.client.ToyBoxUI;
import com.threerings.toybox.util.ToyBoxContext;
import com.threerings.util.MessageBundle;

import cribbage.data.CribbageCodes;

import java.awt.*;
import javax.swing.*;

class CribbagePanel
extends PlacePanel
{
    public CribbageTableView board;
    public JButton play;

    public CribbagePanel(ToyBoxContext ctx, CribbageController ctrl)
    {
        super(ctrl);
        MessageBundle msgs = ctx.getMessageManager().getBundle(CribbageCodes.CRIBBAGE_MSG_BUNDLE);

        // give ourselves a wee bit of a border
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        HGroupLayout gl = new HGroupLayout(HGroupLayout.STRETCH, 10, HGroupLayout.CENTER);
        gl.setOffAxisPolicy(HGroupLayout.STRETCH);
        setLayout(gl);

        // create the board
        board = new CribbageTableView(ctx);
        add(board);

        // create our side panel
        VGroupLayout sgl = new VGroupLayout(VGroupLayout.STRETCH);
        sgl.setOffAxisPolicy(VGroupLayout.STRETCH);
        sgl.setJustification(VGroupLayout.TOP);
        JPanel sidePanel = new JPanel(sgl);

        // add a big fat label
        MultiLineLabel vlabel = new MultiLineLabel(msgs.get("m.title"));
        vlabel.setAntiAliased(true);
        vlabel.setFont(ToyBoxUI.fancyFont);
        sidePanel.add(vlabel, VGroupLayout.FIXED);

        // add a "back" button
        sidePanel.add(createButton(msgs, "m.back_to_lobby", CribbageCodes.BACK_TO_LOBBY),
                      VGroupLayout.FIXED);
        
        Icon icon = new ImageIcon(ctx.loadImage("media/tinyherb.png"));
        TurnDisplay turnDisplay = new TurnDisplay(icon);
        turnDisplay.setWinnerText(msgs.get("m.winner"));
        sidePanel.add(new JLabel(msgs.get("m.turn")), VGroupLayout.FIXED);
        sidePanel.add(turnDisplay, VGroupLayout.FIXED);
        
        play = createButton(msgs, "m.play", CribbageCodes.PLAY);
        play.setEnabled(false);
        sidePanel.add(play, VGroupLayout.FIXED);
        
        SwingUtil.setOpaque(sidePanel, false);
        setOpaque(true);
        setBackground(new Color(0xDAEB9C));

        // add a chat box
        ChatPanel chat = new ChatPanel(ctx);
        chat.removeSendButton();
        sidePanel.add(chat, 1);

        // add our side panel to the main display
        add(sidePanel, HGroupLayout.FIXED);
    }


    private static JButton createButton(MessageBundle msgs, String key, String command)
    {
        JButton button = new JButton(msgs.get(key));
        button.setActionCommand(command);
        button.addActionListener(Controller.DISPATCHER);
        return button;
    }
}
